package org.demo;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.List;

/**
 * 
 */
public class WatchDog {
	public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException {
		guard("D:\\kcdown");
	}

	private static void guard(String watchPath) throws IOException {
		Path dir = new File(watchPath).toPath();
		WatchService watcher = FileSystems.getDefault().newWatchService();
		dir.register(watcher, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE,
				StandardWatchEventKinds.ENTRY_MODIFY);
		System.err.println("start WatchDog ...");
		while (true) {
			WatchKey watchKey = null;
			try {
				watchKey = watcher.take();
				System.err.println("something maybe happend ...");
				dealEvents(watchKey);
			} catch (InterruptedException e) {
				System.err.println(e.getMessage());
				break;
			}
		}
	}

	private static void dealEvents(WatchKey watchKey) {
		if (watchKey == null) {
			System.err.println("nothing happend ...");
		}
		List<WatchEvent<?>> pollEvents = watchKey.pollEvents();
		for (WatchEvent<?> watchEvent : pollEvents) {
			WatchEvent.Kind<?> kind = watchEvent.kind();
			if (kind == StandardWatchEventKinds.OVERFLOW) {
				continue;
			}
			Path filename = (Path) watchEvent.context();
			System.err.format(kind.name() + " # file : %s%n", filename.toFile().getAbsolutePath());
			if (!watchKey.reset()) {
				break;
			}
		}
	}
}